package gov.pbc.nn.core.bdf.view;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.bstek.bdf2.core.CoreJdbcDao;
import com.bstek.bdf2.core.business.IDept;
import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.exception.NoneLoginException;
import com.bstek.bdf2.core.service.IDeptService;
import com.bstek.bdf2.core.service.IUserService;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import gov.pbc.nn.core.bdf.IUserSelect;

/**
 * @author Jacky.gao
 * @since 2013-2-5
 */
public class UserSelect extends CoreJdbcDao {
	private IUserService userService;

	private IDeptService deptService;

	@DataProvider
	public void loadUsers(Page<IUser> page, Criteria criteria, Map<String, Object> map) throws Exception {
		IUser user = ContextHolder.getLoginUser();
		if (user == null) {
			throw new NoneLoginException("Please login first");
		}
		String companyId = user.getCompanyId();
		if (StringUtils.isNotEmpty(getFixedCompanyId())) {
			companyId = getFixedCompanyId();
		}
		if (map != null) {
			String beanId = (String) map.get("beanId");
			if (StringUtils.isNotBlank(beanId)) {
				IUserSelect us = this.getApplicationContext().getBean(beanId, IUserSelect.class);
				Map<String, Object> params = (Map<String, Object>) map.get("params");
				us.loadUsers(page, criteria, params);
			}
		} else {
			userService.loadPageUsers(page, companyId, criteria);
		}

	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public IDeptService getDeptService() {
		return deptService;
	}

	public void setDeptService(IDeptService deptService) {
		this.deptService = deptService;
	}

}
